package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.mpweixin.client.constants.ResultEnum;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserBankCard;
import com.tebiecloud.mpweixin.client.model.vo.PersonalInfoVO;
import com.tebiecloud.mpweixin.client.model.vo.UserBankCardVO;
import com.tebiecloud.mpweixin.server.service.MpweixinUserBankCardService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.server.service.feign.YunBankCardClient;
import com.tebiecloud.yunst.client.model.vo.BandCardVO;
import io.swagger.models.auth.In;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户绑定的银行卡 前端控制器
 * @author tebie
 * @date 2019-09-16
 */
@RestController
@RequestMapping("/mpweixinuserbankcard")
public class MpweixinUserBankCardController {

    @Autowired
    private MpweixinUserBankCardService mpweixinUserBankCardService;

    @Autowired
    YunBankCardClient yunBankCardClient;

    /**
     * 绑定银行卡
     * @param cardNo 卡号,
     * @param identityNo 身份证号,
     * @param name 名称,
     * @param phone 绑定手机号
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "cardNo", required = false) String cardNo,
        @RequestParam(value = "identityNo", required = false) String identityNo,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "phone", required = false) String phone) {

        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        long userId = securityUserDetails.getUserId();
        String[] userName = securityUserDetails.getUsername().split(":");
        String openId = "";
        if(userName.length>=2){
            openId = userName[1];
        }
        //调用远程云商通绑卡接口
        ResultBody<BandCardVO> bandCardVOResultBody = yunBankCardClient.applyBindBankCard(userId,phone,cardNo,name,identityNo,openId);
        if(bandCardVOResultBody!=null && bandCardVOResultBody.getCode() == com.tebiecloud.common.constants.ResultEnum.OK.getCode()){
            BandCardVO bandCardVO = bandCardVOResultBody.getData();
            //把所有卡设置为禁用
            UpdateWrapper<MpweixinUserBankCard> mpweixinUserBankCardUpdateWrapper = new UpdateWrapper<>();
            mpweixinUserBankCardUpdateWrapper.eq("user_id",userId);
            MpweixinUserBankCard mpweixinUserBankCard = new MpweixinUserBankCard();
            mpweixinUserBankCard.setStatus(0);
            mpweixinUserBankCardService.update(mpweixinUserBankCard,mpweixinUserBankCardUpdateWrapper);

            //先查询是否已经绑过的该卡
            QueryWrapper<MpweixinUserBankCard> mpweixinUserBankCardQueryWrapper = new QueryWrapper<>();
            mpweixinUserBankCardQueryWrapper.eq("user_id",userId);
            mpweixinUserBankCardQueryWrapper.eq("card_no",cardNo);
            MpweixinUserBankCard mpweixinUserBankCard1 = mpweixinUserBankCardService.getOne(mpweixinUserBankCardQueryWrapper);
            if(mpweixinUserBankCard1!=null){
                UpdateWrapper<MpweixinUserBankCard> mpweixinUserBankCardUpdateWrapper1 = new UpdateWrapper<>();
                mpweixinUserBankCardUpdateWrapper1.eq("mpweixin_user_bank_card_id",mpweixinUserBankCard1.getMpweixinUserBankCardId());
                MpweixinUserBankCard mpweixinUserBankCard2 = new MpweixinUserBankCard();
                mpweixinUserBankCard2.setStatus(1);
                mpweixinUserBankCardService.update(mpweixinUserBankCard2,mpweixinUserBankCardUpdateWrapper1);
            }else{
                MpweixinUserBankCard entity = new MpweixinUserBankCard();
                entity.setCardNo(cardNo);
                entity.setUserId(userId);
                entity.setIdentityNo(identityNo);
                entity.setName(name);
                entity.setPhone(phone);
                entity.setIcon(bandCardVO.getIcon());
                entity.setBank(bandCardVO.getBank());
                entity.setType(bandCardVO.getType());
                entity.setStatus(1);
                entity.setBind(3);
                mpweixinUserBankCardService.save(entity);
            }
        }else{
            return bandCardVOResultBody;
        }
        return ResultBodyUtils.ok();
    }


    /**
     * 返回银行卡绑定过的数据
     */
    @PostMapping("info")
    public ResultBody<UserBankCardVO> info() {
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        long userId =securityUserDetails.getUserId();
       UserBankCardVO userBankCardVO =  mpweixinUserBankCardService.info(userId);
        return  ResultBodyUtils.ok(userBankCardVO);
    }


}
